<?php

namespace App\Services\Admin;

use App\Models\Vip as VipModel;
use App\Models\VipInfo as VipInfoModel;
use App\Models\UserVipInfo as UserVipInfoModel;
use App\Models\UserVipUseRecord as UserVipUseRecordModel;
use App\Models\UserVip as UserVipModel;

class UserVip extends Base
{
    /**
     * 购买或切换vip 的逻辑
     * @param $vip_id       vip
     * @param $user_id      用户id
     * @param $money        所花费的积分
     */
    public function vip_edit($vip_id, $user_id)
    {
        if (empty($vip_id)) throw new \Exception('請選擇vip');

        $vip = $this->info($vip_id);

        /** vip天数有效期 **/
        $duration = $vip['index']->duration ?? 0;
        if(!empty($vip['index']->activate)) {
            $activate = $vip['index']->activate ?? "";
        }else{
            $activate = null;
        }
        $vip_title = $vip['index']->name ?? "";

        /** 写入个人vip卡 **/
        $user_vip_where = [];
        $user_vip_where[] = ['user_id', $user_id];
        $user_vip_where[] = ['vip_id', $vip_id];

        $user_vip = UserVipModel::where($user_vip_where)->first();

        if (!empty($user_vip)) {

            $update = [];

            $update['user_id'] = $user_id;
            $update['vip_maturity'] = date("Y-m-d 23:59:59", strtotime("+{$duration} day"));
            $update['vip_title'] = $vip_title;
            $update['vip_id'] = $vip_id;
            $update['activate'] = $activate;

            UserVipModel::where($user_vip_where)->update($update);

        } else {

            $vip_model = (new UserVipModel);

            $vip_model->user_id = $user_id;
            $vip_model->vip_maturity = date("Y-m-d 23:59:59", strtotime("+{$duration} day"));
            $vip_model->vip_title = $vip_title;
            $vip_model->vip_id = $vip_id;
            $vip_model->activate = $activate;

            $vip_model->save();

        }

        /** 写入个人vip详细卡  **/
        $info = $vip['info'] ?? [];

        /** 先清空vip **/
        UserVipInfoModel::where($user_vip_where)->delete();

        $vip_info_insert = [];

        foreach ($info as $v) {
            $input = [];
            $input['user_id'] = $user_id;
            $input['vip_id'] = $vip_id;
            $input['class_info_id'] = $v->class_info_id;
            $input['num'] = $v->num;
            $input['is_restriction'] = $v->is_restriction;

            $vip_info_insert[] = $input;
        }

        UserVipInfoModel::insert($vip_info_insert);
    }

    //查詢vip列表
    public function vip_lists()
    {
        return (new VipModel)->where("is_display",1)->orderBy("id", "desc")->get();
    }

    //查詢vip詳細
    public function info($vip_id)
    {
        $info = VipInfoModel::where("vip_id", $vip_id)->get();

        $index = VipModel::where("id", $vip_id)->first();

        return [
            'info' => $info,
            'index' => $index
        ];
    }

    //查詢用戶vip
    public function user_vip($user_id)
    {
        return (new UserVipModel)->where("user_id", $user_id)->get();
    }

    //用户vip详细
    public function user_vip_info($user_id, $vip_id)
    {
        return (new UserVipInfoModel())->with(['class_base_info'])->where("user_id", $user_id)->where("vip_id", $vip_id)->get();
    }

    //用戶使用記錄
    public function vip_use_info($user_id, $vip_id)
    {
        $input = request()->all();

        $where = [];

        if(!empty($input['class_name'])){
            $where[] = ["class_name", "like", "%{$input['class_name']}%"];
        }

        if(!empty($input['start_time']) && !empty($input['end_time'])){
            $where[] = ["created_at",">=", $input['start_time']];
            $where[] = ["created_at","<=", $input['end_time']];
        }

        return (new UserVipUseRecordModel())->where("user_id", $user_id)->where("user_vip_id", $vip_id)->where($where)->orderBy("id","desc")->paginate(15);
    }


    //刪除用戶vip
    public function user_vip_del($user_id, $vip_id)
    {
        (new UserVipModel())->where("user_id", $user_id)->where('vip_id', $vip_id)->delete();
        (new UserVipInfoModel())->where("user_id", $user_id)->where('vip_id', $vip_id)->delete();
    }

    //更改时间
    public static function vip_maturity($param)
    {
        if(!empty($param['expire_time'])) {
            $expire_time = "{$param['expire_time']} 23:59:59";
        }else{
            $expire_time = null;
        }
        return (new UserVipModel())->where('user_id', $param['user_id'])->where('vip_id', $param['vip_id'])->update(['vip_maturity'=>$expire_time]);
    }

    //更改激活時間
    public static function vip_activate($param)
    {
        if(!empty($param['activate'])) {
            $activate = "{$param['activate']} 00:00:00";
        }else{
            $activate = null;
        }
        return (new UserVipModel())->where('user_id', $param['user_id'])->where('vip_id', $param['vip_id'])->update(['activate'=>$activate]);
    }

}
